package com.ddz.qpz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddz.common.exception.Assert;
import com.ddz.common.result.ResponseEnum;
import com.ddz.qpz.mapper.FundMapper;
import com.ddz.qpz.mapper.UserHoldMapper;
import com.ddz.qpz.pojo.entity.Fund;
import com.ddz.qpz.pojo.entity.UserHold;
import com.ddz.qpz.pojo.vo.FundVo;
import com.ddz.qpz.service.UserAccountService;
import com.ddz.qpz.service.UserHoldService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @Author: LiQingBo
 * @Date: 2021/11/1-11-01-18:27
 * @Description: com.ddz.qpz.service.impl
 * @version: 1.0
 */
@Service
public class UserHoldServiceImpl extends ServiceImpl<UserHoldMapper, UserHold> implements UserHoldService {
    @Resource
    private FundMapper fundMapper;
    @Resource
    private UserAccountService userAccountService;

    /**
     * 购买基金
     *
     * @param fundVo
     * @return
     */
    @Override
    public String commitInvest(FundVo fundVo) {

        String fundId = fundVo.getFundId();
        Fund fund = fundMapper.selectById(fundId);
        BigDecimal fundRate = fund.getFundRate();

        //用户余额：当前用户的余额 >= 当前投资金额
        Long investUserId = fundVo.getInvestUserId();
        BigDecimal account = userAccountService.getAccount(investUserId);
        Assert.isTrue(
                account.doubleValue() >= Double.parseDouble(fundVo.getInvestAmount()),
                ResponseEnum.NOT_SUFFICIENT_FUNDS_ERROR);
        //生成投资记录
        UserHold userHold = new UserHold();
        //userId
        userHold.setUid(investUserId);
        //基金代码
        userHold.setFundNumber(fundVo.getFundId());
        //投资金额
        userHold.setHoldMonkey(new BigDecimal(fundVo.getInvestAmount()));
        //昨日利息

//        userHold.setYesterdayIncome();
        /*
        lendItem.setInvestUserId(investUserId);//投资人id
        lendItem.setInvestName(investVO.getInvestName());//投资人名字
        String lendItemNo = LendNoUtils.getLendItemNo();
        lendItem.setLendItemNo(lendItemNo); //投资条目编号（一个Lend对应一个或多个LendItem）
        lendItem.setLendId(investVO.getLendId());//对应的标的id
        lendItem.setInvestAmount(new BigDecimal(investVO.getInvestAmount())); //此笔投资金额
        lendItem.setLendYearRate(lend.getLendYearRate());//年化
        lendItem.setInvestTime(LocalDateTime.now()); //投资时间
        lendItem.setLendStartDate(lend.getLendStartDate()); //开始时间
        lendItem.setLendEndDate(lend.getLendEndDate()); //结束时间
         */

        return null;
    }
}
